package com.lucy.property.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.property.entity.PropertyUnit;
import com.lucy.property.params.UnitCreateParams;
import com.lucy.property.params.UnitQueryParams;
import com.lucy.property.params.UnitUpdateParams;
import com.lucy.property.service.PropertyUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author kindear
 */
@RestController
@Api(tags = "单元管理")
public class UnitController {
    @Autowired
    PropertyUnitService service;

    @PostMapping("units")
    @ApiOperation(value = "单元新增",notes = "")
    public ResponseDTO<Object> unitCreate(@RequestBody @Validated UnitCreateParams params){
        PropertyUnit unit = new PropertyUnit();
        BeanUtil.copyProperties(params,unit, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(
                service.saveOne(unit)
        );
    }

    @DeleteMapping("units/{unitId}")
    @ApiOperation(value = "单元删除",notes = "")
    public ResponseDTO<Object> unitDelete(@PathVariable("unitId") String unitId){
        service.deleteOne(unitId);
        return ResponseDTO.succMsg("删除成功");
    }

    @PutMapping("units")
    @ApiOperation(value = "单元更新",notes = "")
    public ResponseDTO<Object> unitUpdate(@RequestBody @Validated UnitUpdateParams params){
        Optional<PropertyUnit> unitOp = service.getOne(params.getId());
        if (!unitOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        PropertyUnit unit = unitOp.get();
        BeanUtil.copyProperties(params,unit,CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        return ResponseDTO.succData(
                service.updateOne(unit)
        );
    }

    @GetMapping("units")
    @ApiOperation(value = "单元列表",notes = "")
    public ResponseDTO<Object> unitListQuery(UnitQueryParams params){
        Sort sort;
        if("DESC".equalsIgnoreCase(params.getOrder())){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
        /*
        构建 Example
         */
        PropertyUnit unit = new PropertyUnit();
        BeanUtil.copyProperties(params,unit,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<PropertyUnit> example = Example.of(unit);
        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @GetMapping("units/{unitId}")
    @ApiOperation(value = "单元查询",notes = "")
    public ResponseDTO<Object> unitQuery(@PathVariable("unitId") String unitId){
        Optional<PropertyUnit> unitOp = service.getOne(unitId);
        return unitOp.<ResponseDTO<Object>>map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(HttpStatus.NOT_FOUND));
    }

    @GetMapping("units/all")
    @ApiOperation(value = "全部单元",notes = "")
    public ResponseDTO<Object> unitAll(){
        PropertyUnit unit = new PropertyUnit();
        Example<PropertyUnit> example = Example.of(unit);
        List<PropertyUnit> units = service.getExample(example);
        return ResponseDTO.succData(
                units
        );
    }

}
